package com.wg.msproductgz.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wg.msproductgz.entity.Product;
import com.wg.msproductgz.entity.otd.Pro;
import com.wg.msproductgz.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Service
public class ProductService extends ServiceImpl<ProductMapper, Product> {
    @Autowired
    private ProductMapper productMapper;
    public Pro select(Integer id) {
        Product product = productMapper.selectById(id);
        if (product == null) {
            // 可以返回null或抛出自定义异常
            return null;
        }
        return new Pro(product.getId(), product.getShopId(), product.getName(),
                product.getPriceBefore(), product.getPriceAfter(), product.getStatus(),
                product.getCreateTime(), product.getType(), product.getText(),
                product.getImg() != null ? product.getImg().split(",") : null,
                product.getBuyEd(), product.getCount());
    }
    public Product selectOne(Integer  shopId,String name) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id", shopId).like("name", name);
        return productMapper.selectOne(queryWrapper);
    }
    public List<Product> searchProducts(String keyword) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();

        if (keyword != null && !keyword.isEmpty()) {
            queryWrapper.like("name", keyword)
                    .or()
                    .like("type", keyword)
                    .or().like("text", keyword);
        }

        queryWrapper.orderByDesc("create_time");
        return this.list(queryWrapper);
    }
}
